package com.wie.controller;

import com.wie.pojo.Article;
import com.wie.pojo.PageBean;
import com.wie.pojo.Result;
import com.wie.service.ArticleService;
import com.wie.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     * @param article
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Article article) {
        log.info("新增文章：{}", article);
        articleService.add(article);
        return Result.success();
    }

    /**
     * 文章列表查询
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param state
     * @return
     */
    @GetMapping
    public Result<PageBean<Article>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String state
    ) {
        log.info("获取文章数据：{}, {}", pageSize, state);
        PageBean<Article> pageBean = articleService.list(pageNum, pageSize, categoryId, state);
        return Result.success(pageBean);
    }

    /**
     * 更新文章
     * @param article
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Article article) {
        log.info("更新文章：{}", article);
        articleService.update(article);
        return Result.success();
    }

    /**
     * 获取文章详情
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Result detail(Integer id) {
        log.info("获取文章详情： {}", id);
        Article article = articleService.detail(id);
        return Result.success(article);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id) {
        log.info("删除文章： ", id);
        articleService.delete(id);
        return Result.success();
    }

}
